$(document).ready(function(){


    var userstring=localStorage.getItem("user");
    var user=JSON.parse(userstring);

    var stastring=localStorage.getItem("sta");
    var sta=JSON.parse(stastring);

    if(user!=null)
    {
        var logindate=new Date(user[2]);
        var nowdate=new Date();
        if(logindate.setDate(logindate.getDate()+7)>nowdate&&sta==0)
        {
            if(user[1]==1)
            {
                location.href="./OfficeUserMenu.html";
            }
            if(user[1]==2)
            {
                location.href="./AdministratorMenu.html";
            }
        }
    }
    $(".login").children().eq(0).click(function(){
        window.opener=null;
        window.open('about:blank',"_self");
       window.close();
    });


    $(".login").children().eq(1).click(function(){
      
        var Email=$(".logininput .email").val();
        var Password=$(".logininput .password").val();

        $.ajax({
            type:"POST",
            url:"./LoginServlet",
            data:"Email="+Email+"&Password="+Password,
            success:function(json){
                var msg=JSON.parse(json);
                if(msg.flag!="success")
                {
                    $(".alertInfo").text(msg.flag);
                }

                else
                {
                    var istt=$(".main .is7day").is(":checked");
                    if(istt)
                    {
                        var date=new Date();
                        var datestr=date.toString();
                        msg.infoList[2]=datestr;
                        localStorage.setItem("user",JSON.stringify(msg.infoList));
                        localStorage.setItem("sta",0);
                    }

                    if(msg.infoList[1]==1)
                    {
                        location.href="./OfficeUserMenu.html";
                    }
                    if(msg.infoList[1]==2)
                    {
                        location.href="./AdministratorMenu.html";
                    }
                }
            }
        });
    });
});